package com.captain.special.数组;

import java.util.Arrays;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/4 10:52
 */
public class 有序数组的平方977 {
    public static void main(String[] args) {
        new 有序数组的平方977().sortedSquares(new int[]{-4, -1, 0, 3, 10});
    }

    public int[] sortedSquares(int[] nums) {
        int[] ans = new int[nums.length];
        int arrIndex = nums.length - 1;
        int l = 0;
        int r = nums.length - 1;
        while(l <= r) {
            int lVal = nums[l] * nums[l];
            int rVal = nums[r] * nums[r];
            if(lVal < rVal) {
                ans[arrIndex] = rVal;
                r--;
            }else{
                ans[arrIndex] = lVal;
                l++;
            }
            arrIndex--;
        }
        return ans;
    }
}
